package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageInfo<Employee> selectByQo(EmployeeQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.selectByCondition(qo);
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Employee selectByEmpId(Long id) {

        Employee employee = employeeMapper.selectByPrimaryKey(id);

        return employee;
    }

    @Override
    public void saveOrUpdate(Employee employee, Long[] roleIds) {
        //表示是新增
        if (employee.getId()==null){
            //使用MD5对密码进行加密  ,邮箱作为盐
            String md5Pwd = new Md5Hash(employee.getPassword(), employee.getEmail()).toString();
            //将加密后的密码封装给对象
            employee.setPassword(md5Pwd);

            //保存员工信息
            employeeMapper.insert(employee);
        }else{//表示修改

            //删除旧关系  根据员工的id删除
            employeeMapper.deleteRelation(employee.getId());
            //保存员工信息
            employeeMapper.updateByPrimaryKey(employee);
        }
            //添加新的关系
            if(roleIds!=null){
                for(Long Roleid:roleIds){
                    employeeMapper.insertRelation(employee.getId(),Roleid);
                }
            }
    }
    @Override
    public void deleteById(Long EmpId) {
        //先删除关系
        employeeMapper.deleteRelation(EmpId);
        //删除员工信息
        employeeMapper.deleteByPrimaryKey(EmpId);
    }

    @Override
    public Employee login(String username, String password) {

        Employee employee = employeeMapper.selectByNameandPwd(username,password);

        if(employee==null){
            throw new RuntimeException("账号或密码错误!请重新输入");
        }
        //将用户信息存入到域对象中
        UserContext.setCurrentUser(employee);

        //将员工拥有的权限信息查询出来 存入到域对象中 根据员工id查询
        List<Permission> list = permissionMapper.selectByEmpId(employee.getId());

        UserContext.setCurrentempPermission(list);
        return employee;
    }

    @Override
    public void logout() {
        UserContext.setCurrentUser(null);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        employeeMapper.deleteByIds(ids);
    }

    @Override
    public Employee selectByName(Object username) {
        Employee employee = employeeMapper.selectByName(username);
        return employee;
    }

    @Override
    public List<Employee> selectEmpByRoleSns(String... rolesns) {
        List<Employee> list = employeeMapper.selectEmpByRoleSns(rolesns);
        return list;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> list = employeeMapper.selectAll();
        return list;
    }

    @Override
    public void insertEmps(List<Employee> list) {
        for(Employee employee:list){
            employeeMapper.insert(employee);
        }
    }
}
